// import {
//   findProjectInfo,
//   getCompanys,
//   findUserInfo,
//   getProjProgress,
//   getProjandModelNum,
//   listAllWbsIntoStatus
// } from '@/services/kanbanServices';
import { jsapi, viewUtils } from '@/utils/arcgis';
// todo展示数据
import {
  listSectionProgress,
  listTotalProgress,
  qualityAccept,
  listAllWbsIntoStatus,
  taskInspectStatistics,
  getDangerSourceExamine,
} from '@/services/bimServices';

// import { jsapi, viewUtils } from '@/utils/arcgis';
// import { statusArr, sizeArr } from '@/constants/projDoc';
// import moment from 'moment';

// todo 交互
async function createDangerPathLayer(dangerSourceExamine) {
  const lineSymbol = {
    type: 'simple-line', // autocasts as SimpleLineSymbol()
    color: [226, 119, 40],
    width: 4,
  };

  const [Graphic, GraphicsLayer] = await jsapi.load(['esri/Graphic', 'esri/layers/GraphicsLayer']);
  const routeLayer = new GraphicsLayer({
    id: 'dangerSourceExamineLayer',
    title: 'dangerSourceExamineLayer',
  });

  const graphics = dangerSourceExamine.map(item => {
    const { locationList, ...attr } = item;
    let cfg = {
      geometry: {
        type: 'polyline', // autocasts as new Polyline()
        paths: locationList.map(({ longitude, latitude }) => {
          return [longitude, latitude];
        }),
      },
      symbol: lineSymbol,
      attributes: attr,
    };
    console.log(cfg);
    return new Graphic(cfg);
  });
  const view = await viewUtils.isViewReady();
  view.map.add(routeLayer);
  console.log(graphics);
  routeLayer.graphics = graphics;
  return routeLayer;
  // view.goTo(graphics[0]);
}

// 项目级看板展示需要的数据,因为需要做一些数据处理故放在这里
const ProjectPage = {
  namespace: 'projectPage',

  state: {
    totalProgress: [], // 分项进度
    sectionProgress: [], // 分项进度
    dangerSourceExamine: [], // 巡检路线
    allWbs: null, //  按照不同验收分好类的全部wbs code
    selectedWbs: '', // 当前选中的wbs点位
    selectedId: '', // 当前选中的id点位 用于质量等查询
    curVideoId: '', // 图层中获取的视频id
    urlFromCameraLayer: '', // 图层中获取的当前视频url
    statisticsData: null, // 数据大杂烩
    // sectionProgressLast:[], // 最新的分项
  },

  effects: {
    // 获取分项进度列表
    *getSectionProgress({ payload }, { put, select }) {
      const curProjId = yield select(store => store.app.curProjId);
      const data = yield listSectionProgress(curProjId);

      const obj = data.reduce((r, e) => {
        if (!r[e.wbsSectionName]) {
          r[e.wbsSectionName] = [];
        }
        r[e.wbsSectionName].push(e);
        return r;
      }, {});

      const sectionArr = Object.entries(obj).map(([k, v]) => {
        return {
          title: k,
          items: v,
          lastData: v.slice(-1)[0],
        };
      });

      if (sectionArr.length > 0) {
        // 更新model里的值
        yield put({
          type: 'updateState',
          payload: {
            sectionProgress: sectionArr,
          },
        });
      }
    },
    *getTotalProgress({ payload }, { put, select }) {
      const curProjId = yield select(store => store.app.curProjId);
      const data = yield listTotalProgress(curProjId);
      if (data.length > 0) {
        // 更新model里的值
        yield put({
          type: 'updateState',
          payload: {
            totalProgress: data,
          },
        });
      }
    },
    *getAllWbs({ payload }, { put, select }) {
      const curProjId = yield select(store => store.app.curProjId);
      let data;
      try {
        data = yield listAllWbsIntoStatus(curProjId);
      } catch (error) {
        data = {
          notStartList: 'api错位占位',
          doingList: 'api错位占位',
          notCalculateList: 'api错位占位',
          calculatedList: 'api错位占位',
        };
      }
      yield put({
        type: 'updateState',
        payload: {
          allWbs: data,
        },
      });
    },
    *getStatisticsData({ payload }, { put, select }) {
      const curProjId = yield select(store => store.app.curProjId);
      const data = yield taskInspectStatistics(curProjId);
      yield put({
        type: 'updateState',
        payload: {
          statisticsData: data,
        },
      });
    },

    *listDangerSourceExamine({ payload }, { put, select, call }) {
      const curProjId = yield select(store => store.app.curProjId);
      const data = yield getDangerSourceExamine(curProjId);
      yield put({
        type: 'updateState',
        payload: {
          dangerSourceExamine: data,
        },
      });
      // let lyr = yield call(createDangerPathLayer, data);
      // return lyr;
    },
  },

  reducers: {
    updateState(state, { payload }) {
      return { ...state, ...payload };
    },
  },
};

export default ProjectPage;
